package fzrj.qkl.demo.controller;

import fzrj.qkl.demo.model.CommonResponse;
import fzrj.qkl.demo.service.backend.impl.IUserBackendService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserBackendService userBackendService;

    // 注册
    @PostMapping(value = "/add")
    public CommonResponse addUser(@RequestParam("name") String name) {
        boolean ok = userBackendService.addUser(name);
        if (ok) {
            return CommonResponse.ok(ok);
        }
        return CommonResponse.fail("1",new Exception("error"));
    }

    // 验证
    @PostMapping(value = "/check")
    public CommonResponse checkUser(@RequestParam("id") int id) throws ContractException {
        boolean ok = userBackendService.checkUser(id);
        if (ok) {
            return CommonResponse.ok(ok);
        }
        return CommonResponse.fail("1",new Exception("error"));
    }

}
